package com.cwises.action.shop;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.core.base.BaseAction;
import com.cwises.entity.shop.ShopData;
import com.cwises.entity.shop.ShopExamine;
import com.cwises.entity.shop.ShopInfo;
import com.cwises.entity.shop.ShopShowImg;
import com.cwises.entity.shop.StypeInfo;
import com.cwises.entity.system.user.UserInfoManager;
import com.cwises.service.shop.ShopService;
import com.util.FileIO;
import com.util.GetConfig;

@Controller
public class ShopAction extends BaseAction {

	@Resource(name = "shopService")
	private ShopService shopService;

	private ShopInfo shopInfo;
	private ShopData shopData;
	private ShopExamine shopExamine;
	private List<ShopShowImg> shopImgList;

	/* 当前页号及每页数 */
	private int currentPage;
	private int pageSize;

	private String actionResult;
	private String actionTitle = "商品管理";

	private List<StypeInfo> stypes;
	
	@SuppressWarnings("unchecked")
	public String findShop() {
		// 判断是否有shopId
		String shopId = getShopId();
		if (shopId == null || "".equals(shopId)) {
			String userId=getCurUserId();
			shopExamine = (ShopExamine) shopService.findObjectByCondition("findByUserId",userId);
			if (shopExamine != null) {
				String examine_id = shopExamine.getId();
				shopInfo = (ShopInfo) shopService.findObjectByCondition("getExamineInfo", examine_id);
				String shop_examine_id=shopInfo.getId();
				stypes = shopService.findByCondition("findExamineStypes", shop_examine_id);
				String stypeNames="";
				for(StypeInfo stype:stypes){
					stypeNames+=stype.getT_name()+",";
				}
				shopInfo.setStype(stypeNames);
				shopInfo.setStatus("59");
				stypeNames=stypeNames.substring(0,stypeNames.length()-1);
				shopImgList = (List<ShopShowImg>) shopService.findByCondition("findExamineImg", examine_id);
				shopData = (ShopData) shopService.findObjectByCondition("findExamineData", examine_id);
				return "examineDetail";
			} else {
				//stypes = shopService.findByCondition("findStypes", null);
				return "gotoAdd";
			}
		} else {
			Map<String,Object> examineCount=shopService.findObjectByCondition("findExamineCount", shopId);
			long examine_info=examineCount.get("examine_info")==null?0:(long)examineCount.get("examine_info");
			long examine_data=examineCount.get("examine_data")==null?0:(long)examineCount.get("examine_data");
			long examine_img=examineCount.get("examine_img")==null?0:(long)examineCount.get("examine_img");
			// 查询实体 对应map中findById
			shopInfo = (ShopInfo) shopService.findById(shopId);
			shopInfo.setExamine_info(examine_info);
			shopInfo.setExamine_data(examine_data);
			shopInfo.setExamine_img(examine_img);
			shopImgList = (List<ShopShowImg>) shopService.findByCondition("findShopShowImg", shopId);
			shopData = (ShopData) shopService.findObjectByCondition("findShopData", shopId);
			shopExamine=null;
			stypes = shopService.findByCondition("findStypesByShopId", shopId);
			String stypeNames="";
			for(StypeInfo stype:stypes){
				stypeNames+=stype.getT_name()+",";
			}
			stypeNames=stypeNames.substring(0,stypeNames.length()-1);
			shopInfo.setStype(stypeNames);
		}
		return "success";
	}
	
	@SuppressWarnings("unchecked")
	public String gotoAdd() {
		stypes = shopService.findByCondition("findStypes", null);
		return "success";
	}
	
	public void insertByCode() throws IOException {
		String invitation = getParameter("invitation");
		String userId=getCurUserId();
		String shopId=shopService.findObjectByCondition("findIdByInvitation", invitation);
		String result = "success";
		if(shopId==null||"".equals(shopId)){
			result="noCode";
		}else{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("shopId", shopId);
			map.put("userId", userId);
			map.put("updatePerson", userId);
			map.put("updateTime", new Date());
			try{
				shopService.updateByCondition("updateUserShop", map);
				UserInfoManager user = getCurUser();
				user.setShopId(shopId);
				setCurUser(user);
			}catch(Exception e){
				e.printStackTrace();
				result = "error";
			}
		}
		ServletActionContext.getResponse().getWriter().write(result);
	}

	/**
	 * @Description: 添加
	 * @return
	 * @return String
	 * @throws
	 */
	public String insert() {
		String flag = "success";
		// 商户ID
		String shopId = UUID.randomUUID().toString();
		// 审核Id
		String examineId = UUID.randomUUID().toString();
		// 审核信息
		ShopExamine shopExamine = new ShopExamine();
		shopExamine.setId(examineId);
		shopExamine.setShop_id(shopId);
		shopExamine.setExamine_type("1");
		shopExamine.setExamine_result("1");
		shopExamine.setCreate_person(getCurUserId());
		shopExamine.setCreate_time(new Date());
		// 审核商户详情
		shopInfo.setShop_id(shopId);
		shopInfo.setId(UUID.randomUUID().toString());
		shopInfo.setExamine_id(examineId);
		try {
			String base = GetConfig.getFileoutputpath();
			String imgPath = "shop/" + shopId;
			String temp_view = shopInfo.getImg_view();
			if (temp_view != null && !"".equals(temp_view)) {
				String temp_path = base + temp_view;
				String img_view = FileIO.moveFile(imgPath, temp_path);
				shopInfo.setImg_view(img_view);
			}
			// 审核图片详情
			String addImglist = getParameter("addList");
			List<ShopShowImg> addShowImg = getAddShowImg(addImglist,shopId, examineId);
			// 审核商户银行信息
			shopData.setId(UUID.randomUUID().toString());
			shopData.setShop_id(shopId);
			shopData.setExamine_id(examineId);
			// 新增实体 对应map中add
			shopService.insertShopInfo(shopExamine, shopInfo, addShowImg,shopData);
		} catch (Exception e) {
			flag = "error";
			e.printStackTrace();
		}
		// 返回操作结果
		setActionResult(flag);
		return flag;
	}

	/**
	 * @Description: 进入修改页面
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public String gotoModify() {
		String shopId=getShopId();
		shopInfo = (ShopInfo) shopService.findById(shopId);
		stypes = shopService.findByCondition("findStypes", null);
	   	List<String> shopType=shopService.findByCondition("findShopTypes", shopId);
	   	String shopTypes="";
	   	for(String type:shopType){
	   		shopTypes+=type+",";
	   	}
	   	shopInfo.setStype(shopTypes);
		return "success";
	}

	/**
	 * @Description:修改
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	public String update() {
		String flag = "success";
		String temp_view = "";
		String examineId=UUID.randomUUID().toString();
		String shopId=getShopId();
		String userId=getCurUserId();
		// 审核信息
		ShopExamine shopExamine = new ShopExamine();
		shopExamine.setId(examineId);
		shopExamine.setShop_id(shopId);
		shopExamine.setExamine_type("2");
		shopExamine.setExamine_result("1");
		shopExamine.setCreate_person(userId);
		shopExamine.setCreate_time(new Date());
		try {
			shopInfo.setId(UUID.randomUUID().toString());
			shopInfo.setShop_id(shopId);
			shopInfo.setExamine_id(examineId);
			
			temp_view = shopInfo.getImg_view();
			if (temp_view != null && !"".equals(temp_view)) {
				String base = GetConfig.getFileoutputpath();
				String temp_path = base + temp_view;
				String img_view = FileIO.moveFile("shop/" + shopId,temp_path);
				shopInfo.setImg_view(img_view);
			}else{
				shopInfo.setImg_view(getParameter("oldImgView"));
			}
			// 对应map中update
			shopService.updateInfo(shopExamine,shopInfo);
		} catch (Exception e) {
			flag = "error";
			e.printStackTrace();
		}
		// 返回操作结果
		setActionResult(flag);
		return "success";
	}

	/**
	 * @Description: 验证名称是否重复
	 * @return void
	 * @throws
	 */
	public void validateName() throws IOException {
		String id = getParameter("id");
		String sName = getParameter("s_name");
		sName = new String(sName.getBytes("iso8859-1"), "utf-8");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("s_name", sName);
		Boolean result = true;
		StypeInfo stype = (StypeInfo) shopService.findObjectByCondition("getShop", map);
		if (stype != null)
			result = false;
		ServletActionContext.getResponse().getWriter().write(result.toString());
	}

	/**
	 * @Description: 进入修改商户详情图片信息页面
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public String gotoModifyShowImg() {
		shopImgList = (List<ShopShowImg>) shopService.findByCondition("findShopShowImg", getShopId());
		return "success";
	}

	/**
	 * @Description: 修改商户详情图片信息
	 * @return String
	 */
	public String updateShowImg() {
		// 页面跳转及返回信息
		String flag = "success";
		// 审核信息
		String examineId = UUID.randomUUID().toString();
		String shopId = getShopId();
		ShopExamine shopExamine = new ShopExamine();
		shopExamine.setId(examineId);
		shopExamine.setShop_id(shopId);
		shopExamine.setExamine_type("3");
		shopExamine.setExamine_result("1");
		shopExamine.setCreate_person(getCurUserId());
		shopExamine.setCreate_time(new Date());
		String addImglist = getParameter("addList");
		try {
			List<ShopShowImg> addShowImg = getAddShowImg(addImglist,shopId, examineId);
			shopService.updateShowImg(shopExamine, addShowImg);
		} catch (Exception e) {
			flag = "error";
			e.printStackTrace();
		}
		// 返回操作结果
		setActionResult(flag);
		return "success";
	}

	public List<ShopShowImg> getAddShowImg(String addImglist,String shopId, String examineId) throws IOException {
		String base = GetConfig.getFileoutputpath();
		String imgPath = "shop/" + shopId;
		List<ShopShowImg> addShowImg = new ArrayList<ShopShowImg>();
		if (addImglist.length() > 2) {
			String[] addImg = addImglist.split(",");
			for (int i = 0; i < addImg.length; i++) {
				if (!"".equals(addImg[i]) && addImg[i] != null) {
					String[] imgIdNum = addImg[i].split("#");
					if (imgIdNum[0] != null && !"".equals(imgIdNum[0])) {
						ShopShowImg shopShowImg = new ShopShowImg();
						shopShowImg.setId(imgIdNum[0]);
						shopShowImg.setShop_id(shopId);
						shopShowImg.setExamine_id(examineId);
						shopShowImg.setImg_type("2");
						String img_view =imgIdNum[1];
						if("add".equals(imgIdNum[2])){
							img_view = FileIO.moveFile(imgPath, base+ imgIdNum[1]);
						}
						shopShowImg.setImg_view(img_view);
						shopShowImg.setImg_num(i);
						addShowImg.add(shopShowImg);
					}
				}
			}
		} else {
			addShowImg = null;
		}
		return addShowImg;
	}

	/**
	 * @Description: 进入修改支付信息页面
	 * @return String
	 */
	public String gotoModifyAplay() {
		shopData = (ShopData) shopService.findObjectByCondition("findShopData",getShopId());
		return "success";
	}

	/**
	 * @Description: 进入修改支付信息
	 * @return String
	 */
	public String updateAplay() {
		String flag = "success";
		// 审核信息
		String examineId = UUID.randomUUID().toString();
		String shopId = getShopId();
		ShopExamine shopExamine = new ShopExamine();
		shopExamine.setId(examineId);
		shopExamine.setShop_id(shopId);
		shopExamine.setExamine_type("4");
		shopExamine.setExamine_result("1");
		shopExamine.setCreate_person(getCurUserId());
		shopExamine.setCreate_time(new Date());
		try {
			shopData.setId(UUID.randomUUID().toString());
			shopData.setShop_id(shopId);
			shopData.setExamine_id(examineId);
			// 修改
			shopService.updateAplay(shopExamine,shopData);
		} catch (Exception e) {
			flag = "error";
			e.printStackTrace();
		}
		// 返回操作结果
		setActionResult(flag);
		return "success";
	}

	/**
	 * @Description: 进入修改商户认证信息页面
	 * @return String
	 */
	public String gotoModifyCftImg() {
		shopData = (ShopData) shopService.findObjectByCondition("findShopData",getShopId());
		return "success";
	}

	/**
	 * @Description: 修改支付信息
	 * @return String
	 */
	public String updateCftImg() {
		return "success";
	}

	/**
	 * @Description: 进入修改详情申请详情页面
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public String gotoExamineInfo() {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("shop_id", getShopId());
		params.put("examine_type", "2");
		shopExamine=shopService.findObjectByCondition("findExamineByType", params);
		String examine_id=shopExamine.getId();
		shopInfo = (ShopInfo) shopService.findObjectByCondition("getExamineInfo", examine_id);
		String shop_examine_id=shopInfo.getId();
		stypes = shopService.findByCondition("findExamineStypes", shop_examine_id);
		String stypeNames="";
		for(StypeInfo stype:stypes){
			stypeNames+=stype.getT_name()+",";
		}
		stypeNames=stypeNames.substring(0,stypeNames.length()-1);
		shopInfo.setStype(stypeNames);
		return "success";
	}
	/**
	 * @Description: 进入修改商户详情图片信息申请详情页面
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public String gotoExamineImg() {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("shop_id", getShopId());
		params.put("examine_type", "3");
		shopExamine=shopService.findObjectByCondition("findExamineByType", params);
		String examine_id=shopExamine.getId();
		shopImgList = (List<ShopShowImg>) shopService.findByCondition("findExamineImg", examine_id);
		return "success";
	}

	/**
	 * @Description: 进入修改支付信息申请详情页面
	 * @return String
	 */
	public String gotoExamineAplay() {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("shop_id", getShopId());
		params.put("examine_type", "4");
		shopExamine=shopService.findObjectByCondition("findExamineByType", params);
		String examine_id=shopExamine.getId();
		shopData = (ShopData) shopService.findObjectByCondition("findExamineData",examine_id);
		return "success";
	}

	public ShopInfo getShopInfo() {
		return shopInfo;
	}

	public void setShopInfo(ShopInfo shopInfo) {
		this.shopInfo = shopInfo;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getActionResult() {
		return actionResult;
	}

	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	public String getActionTitle() {
		return actionTitle;
	}

	public void setActionTitle(String actionTitle) {
		this.actionTitle = actionTitle;
	}

	public List<StypeInfo> getStypes() {
		return stypes;
	}

	public void setStypes(List<StypeInfo> stypes) {
		this.stypes = stypes;
	}

	public ShopData getShopData() {
		return shopData;
	}

	public void setShopData(ShopData shopData) {
		this.shopData = shopData;
	}

	public List<ShopShowImg> getShopImgList() {
		return shopImgList;
	}

	public void setShopImgList(List<ShopShowImg> shopImgList) {
		this.shopImgList = shopImgList;
	}

	public ShopExamine getShopExamine() {
		return shopExamine;
	}

	public void setShopExamine(ShopExamine shopExamine) {
		this.shopExamine = shopExamine;
	}
}
